// 组件公用 behavior
const { DEFAULT_MODE, DEFAULT_THEME, DEFAULT_STYLE_MODE } = require('../config');
const { BUCKET_RESOURCE } = require('../constants/common');
const { getMode, getTheme, getStyleMode } = require('../biztools/info');

// 组件参数
const COMPONENT_OPTIONS = {
    addGlobalClass: true,
    multipleSlots: true,
};

const BaseBehavior = Behavior({
    properties: {
        // 模式
        mode: {
            type: String,
            value: getMode() || DEFAULT_MODE,
        },
        // 主题色
        theme: {
            type: String,
            value: getTheme() || DEFAULT_THEME,
        },
        // 主体样式
        styleMode: {
            type: String,
            value: getStyleMode() || DEFAULT_STYLE_MODE,
        },
        // 用户自定义样式 class
        customClass: {
            type: String,
            value: '',
        },
    },
    data: {
        _bucket: BUCKET_RESOURCE,
    },
});

const SelectQueryBehavior = Behavior({
    methods: {
        // 获取单个元素的矩形长宽
        getRect(selector) {
            return new Promise((resolve, reject) => {
                this.createSelectorQuery()
                    .select(selector)
                    .boundingClientRect(rect => {
                        if (rect) {
                            resolve(rect)
                        } else {
                            reject(new Error(`can not find selector: ${selector}`))
                        }
                    }).exec();
            })
        },
        // 获取所有元素的矩形长宽
        getAllRects(selector) {
            return new Promise((resolve, reject) => {
                this.createSelectorQuery()
                    .selectAll(selector)
                    .boundingClientRect(rects => {
                        if (rects && rects.lenght > 0) {
                            resolve(rects)
                        } else {
                            reject(new Error(`can not find selector: ${selector}`))
                        }
                    }).exec();
            })
        }
    },
});

module.exports = {
    COMPONENT_OPTIONS,
    BaseBehavior,
    SelectQueryBehavior,
};